// Dom7
var $$ = Dom7;

// Framework7 App main instance
var app = new Framework7({
	root: '#app', // App root element
	id: 'io.yachtar.app', // App bundle ID
  	name: 'Yachtar', // App name
	theme: 'auto', // Automatic theme detection
	dialog: {
		// set default title for all dialog shortcuts
		title: '提示',
		// change default "OK" button text
		buttonOk: '确认',
	  }
});

// Init/Create main view
var mainView = app.views.create('.view', {
	url: '/'
});
var housedatas;
var ticketflag;
//var cardid = "330382199306097115";
var serialno = sessionStorage.getItem("serialno");
var givetype = sessionStorage.getItem("givetype");
var cardid = sessionStorage.getItem("cardid");
var ty = document.getElementById('ty');
var fd = document.getElementById('fd');
var qq = document.getElementById('qq');
$(document).ready(function(){
	call_remote(REQUEST_TYPE830,{cardid:cardid,serialno:serialno,givetype:givetype},function(data){
		housedatas = data;
		if(data.length>0){
			for (var i = 0; i < data.length; i++) {
				var elem_div = document.createElement("div");
				elem_div.setAttribute("class", "house-bg");
				elem_div.innerHTML = "<div class='house-content'>"
				+"	<div class='row houserow'>"
				+"		<div class='col-30 houselabel'>坐落:</div>"
				+"		<div class='col-70 housedata'>"+data[i].location+"</div>"
				+"  </div>"
				+"	<div class='row houserow'>"
				+"		<div class='col-30 houselabel'>面积:</div>"
				+"		<div class='col-40 housedata'>"+data[i].area+"</div>"
				+"		<div class='col-30 houseunit'>平方米</div>"
				+"	</div>"
				+"	<div class='row houserow'>"
				+"		<div class='col-30 houselabel'>分摊系数:</div>"
				+"		<div class='col-40 housedata'>"+data[i].devertrate+"</div>"
				+"		<div class='col-30 houseunit'>元/平方米</div>"
				+"	</div>"
				+"	<div class='row houserow'>"
				+"		<div class='col-30 houselabel'>分摊金额:</div>"
				+"		<div class='col-40 housedata'>"+data[i].devert+"</div>"
				+"		<div class='col-30 houseunit'>元</div>"
				+"	</div>"
				+"</div>";
				document.getElementById('houselist').appendChild(elem_div); 
			}
		}else{
			var elem_div = document.createElement("div");
			elem_div.setAttribute("class", "house-bg");
			elem_div.innerHTML = "<div class='house-content'>"
				+"	<div class='row houserow'>"
				+"		<div class='col-30 houselabel'>坐落:</div>"
				+"		<div class='col-70 housedata'>"+data[i].location+"</div>"
				+"  </div>"
				+"	<div class='row houserow'>"
				+"		<div class='col-30 houselabel'>面积:</div>"
				+"		<div class='col-40 housedata'>"+data[i].area+"</div>"
				+"		<div class='col-30 houseunit'>平方米</div>"
				+"	</div>"
				+"	<div class='row houserow'>"
				+"		<div class='col-30 houselabel'>分摊系数:</div>"
				+"		<div class='col-40 housedata'>"+data[i].devertrate+"</div>"
				+"		<div class='col-30 houseunit'>元/平方米</div>"
				+"	</div>"
				+"	<div class='row houserow'>"
				+"		<div class='col-30 houselabel'>分摊金额:</div>"
				+"		<div class='col-40 housedata'>"+data[i].devert+"</div>"
				+"		<div class='col-30 houseunit'>元</div>"
				+"	</div>"
				+"</div>";
				document.getElementById('houselist').appendChild(elem_div); 
		}
	});
	ty.style.backgroundImage = 'url(../../img/xz.png)';
	ty.style.color = "white";
	fd.style.backgroundImage = 'url(../../img/wxz.png)';
	fd.style.color = "#696969";
	qq.style.backgroundImage = 'url(../../img/wxz.png)';
	qq.style.color = "#696969";
	ticketflag = "Y";
});


//返回
$$('#backimg').on('click', function () {
	window.location.href = "../../plugin/vote/votedetail.html";
});

$$('#ty').on('click', function () {
	ty.style.backgroundImage = 'url(../../img/xz.png)';
	ty.style.color = "white";
	fd.style.backgroundImage = 'url(../../img/wxz.png)';
	fd.style.color = "#696969";
	qq.style.backgroundImage = 'url(../../img/wxz.png)';
	qq.style.color = "#696969";
	ticketflag = "Y";
});

$$('#fd').on('click', function () {
	ty.style.backgroundImage = 'url(../../img/wxz.png)';
	ty.style.color = "#696969";
	fd.style.backgroundImage = 'url(../../img/xz.png)';
	fd.style.color = "white";
	qq.style.backgroundImage = 'url(../../img/wxz.png)';
	qq.style.color = "#696969";
	ticketflag = "N";
});

$$('#qq').on('click', function () {
	ty.style.backgroundImage = 'url(../../img/wxz.png)';
	ty.style.color = "#696969";
	fd.style.backgroundImage = 'url(../../img/wxz.png)';
	fd.style.color = "#696969";
	qq.style.backgroundImage = 'url(../../img/xz.png)';
	qq.style.color = "white";
	ticketflag = "I";
});

$$('#submit').on('click', function () {
	var dataArray = [];
	for (var i = 0; i < housedatas.length; i++) {
		housedatas[i].telphone = $('#telphone').val();
		housedatas[i].ticketflag = ticketflag;
	}
	dataArray.push(housedatas);
	call_remote(REQUEST_TYPE834, {data:housedatas},function(data){
		app.dialog.alert("投票成功！");
		window.location.href = "../../plugin/vote/votedetail.html";
	});
});

